﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ComboBoxAdapter.cs" company="">
//   
// </copyright>
// <summary>
//   The combo box adapter.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Libium.Adapters
{
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;

    using Libium.Views;

    /// <summary>
    /// The combo box adapter.
    /// </summary>
    public class ComboBoxAdapter : DisabledWhenReadonlyControlAdapter<ComboBox>, IChoiceView
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ComboBoxAdapter"/> class.
        /// </summary>
        /// <param name="comboBox">
        /// The combo box.
        /// </param>
        public ComboBoxAdapter(ComboBox comboBox)
            : base(comboBox)
        {
            Control.SelectedValueChanged += OnSelectedValueChanged;
            Control.DropDownStyle = ComboBoxStyle.DropDownList;
        }

        #endregion

        #region Public Events

        /// <summary>
        /// The selected value changed.
        /// </summary>
        public event Action<IChoiceView> SelectedValueChanged;

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets the named values.
        /// </summary>
        public IEnumerable<INamedValue> NamedValues
        {
            get
            {
                return (IEnumerable<INamedValue>)Control.DataSource;
            }

            set
            {
                Control.DataSource = value;
                Control.ValueMember = "Value";
                Control.DisplayMember = "Name";
            }
        }

        /// <summary>
        /// Gets or sets the selected value.
        /// </summary>
        public object SelectedValue
        {
            get
            {
                return Control.SelectedValue;
            }

            set
            {
                Control.SelectedValue = value;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// The on selected value changed.
        /// </summary>
        /// <param name="source">
        /// The source.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        protected virtual void OnSelectedValueChanged(object source, EventArgs e)
        {
            if (SelectedValueChanged != null)
            {
                SelectedValueChanged(this);
            }
        }

        #endregion
    }
}